package com.gitee.zycra.future.job.web.controller;

import com.gitee.zycra.future.job.common.bo.CommonResult;
import com.gitee.zycra.future.job.common.bo.PageQuery;
import com.gitee.zycra.future.job.common.enums.JobCycleEnum;
import com.gitee.zycra.future.job.common.param.AddMetaDataParam;
import com.gitee.zycra.future.job.common.param.PageQueryMetaDataParam;
import com.gitee.zycra.future.job.common.param.UpdateMetaDataParam;
import com.gitee.zycra.future.job.common.vo.JobMetaDataVO;
import com.gitee.zycra.future.job.service.JobMetaDataService;
import com.gitee.zycra.future.job.web.annotation.AuthCheck;
import com.gitee.zycra.jdbc.model.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author zycra
 */
@Slf4j
@RestController
@RequestMapping("/metadata")
public class JobMetaDataController {

    @Autowired
    private JobMetaDataService jobMetaDataService;

    /**
     * 添加
     *
     * @param param 元数据对象
     * @return 添加结果
     */
    @AuthCheck({"6", "7"})
    @PostMapping("/addMetaData")
    public CommonResult<String> addMetaData(@RequestBody @Validated AddMetaDataParam param) {
        JobCycleEnum jobCycle = JobCycleEnum.getByCode(param.getJobCycle());
        if (JobCycleEnum.MINUTE.equals(jobCycle)) {
            param.setJobRetryTimes(0);
            param.setJobRetryInterval(0);
            jobMetaDataService.insertJobMetaData(param);
            return CommonResult.success();
        }
        boolean checkSuccess = retryCheck(jobCycle, param.getJobRetryTimes(), param.getJobRetryInterval());
        if (!checkSuccess) {
            return CommonResult.failed("重试次数过多或间隔过长");
        }
        jobMetaDataService.insertJobMetaData(param);
        return CommonResult.success();
    }

    /**
     * 删除
     *
     * @param id 主键
     * @return 删除结果
     */
    @AuthCheck({"6", "7"})
    @PostMapping("/deleteMetaData")
    public CommonResult<String> deleteMetaData(Long id) {
        return jobMetaDataService.deleteJobMetaData(id);
    }

    /**
     * 更新
     *
     * @param param 元数据对象
     * @return 更新结果
     */
    @AuthCheck({"6", "7"})
    @PostMapping("/updateMetaData")
    public CommonResult<String> updateMetaData(@RequestBody @Validated UpdateMetaDataParam param) {
        JobCycleEnum jobCycle = JobCycleEnum.getByCode(param.getJobCycle());
        if (JobCycleEnum.MINUTE.equals(jobCycle)) {
            param.setJobRetryTimes(0);
            param.setJobRetryInterval(0);
            jobMetaDataService.updateJobMetaData(param);
            return CommonResult.success();
        }
        boolean checkSuccess = retryCheck(jobCycle, param.getJobRetryTimes(), param.getJobRetryInterval());
        if (!checkSuccess) {
            return CommonResult.failed("重试次数过多或间隔过长");
        }
        jobMetaDataService.updateJobMetaData(param);
        return CommonResult.success();
    }

    /**
     * 分页查询
     *
     * @param pageQuery 查询参数
     * @return 查询结果
     */
    @AuthCheck({"4", "5", "6", "7"})
    @PostMapping("/pageList")
    public CommonResult<PageResult<JobMetaDataVO>> pageList(@RequestBody @Validated PageQuery<PageQueryMetaDataParam> pageQuery) {
        PageResult<JobMetaDataVO> pageResult = jobMetaDataService.selectJobMetaDataForPage(pageQuery);
        return CommonResult.success(pageResult);
    }

    /**
     * 检查重试次数和间隔
     *
     * @param jobCycle         执行周期
     * @param jobRetryTimes    重试次数
     * @param jobRetryInterval 重试间隔
     * @return 是否检查通过
     */
    private boolean retryCheck(JobCycleEnum jobCycle, Integer jobRetryTimes, Integer jobRetryInterval) {
        return (jobRetryTimes + 1) * jobRetryInterval <= jobCycle.getMinutes();
    }
}
